package com.lemei.cn.myokhttpbeen.ui.fragment;

import android.os.SystemClock;
import android.view.View;
import android.widget.ListView;

import com.lemei.cn.myokhttpbeen.R;
import com.lemei.cn.myokhttpbeen.adpter.HomeAdpter;
import com.lemei.cn.myokhttpbeen.adpter.MyBesicAdapter;
import com.lemei.cn.myokhttpbeen.been.HomeItem;
import com.lemei.cn.myokhttpbeen.cachemanger.CacheManger;
import com.lemei.cn.myokhttpbeen.pullrefresh.PullToRefreshBase;
import com.lemei.cn.myokhttpbeen.pullrefresh.PullToRefreshListView;
import com.lemei.cn.myokhttpbeen.utils.Uris;
import com.lemei.cn.myokhttpbeen.utils.Utils;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by 赵航 on 2017/6/6.
 */

public class HomeFragment extends BeasicFragment {
    @Bind(R.id.lv_home_list_layout)
    PullToRefreshListView lvHomeListLayout;
    private List<HomeItem.HomeItemBeen> mShowItems = new ArrayList<>();
    private MyBesicAdapter mHomeAdapter;

    @Override
    public View creatSuccessView() {
        View view = View.inflate(getContext(), R.layout.home_show_layout, null);
        ButterKnife.bind(HomeFragment.this, view);
        mHomeAdapter = new HomeAdpter(mShowItems);
        //设置adpter
        lvHomeListLayout.getRefreshableView().setAdapter(mHomeAdapter);
        //        设置当前上拉加载更多是否可用
        lvHomeListLayout.setPullLoadEnabled(true);
        //        设置当前下拉刷新是否可用
        lvHomeListLayout.setPullRefreshEnabled(true);
        //        滑动到底部是否自动加载更多数据
        //   lvHomeListLayout.setScrollLoadEnabled(true);


        lvHomeListLayout.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                if (mShowItems != null) {
                    mShowItems.clear();
                }
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        SystemClock.sleep(2000);
                        requestNetData();
                    }
                }).start();

            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {

            }
        });
        return view;
    }

    @Override
    protected Object requestNetData() {
        //传递数据
        //地址http://127.0.0.1:8090/home?index=0
        //数据格式是Been
        HomeItem homeItem = CacheManger.getInstance().getUrlDataBeen(Uris.HOMEURI, HomeItem.class);


        if (homeItem == null) {


            return null;
        }
        mShowItems.addAll(homeItem.getList());
        //获取数据完成后，要更新ui
        Utils.runOnUIThread(new Runnable() {
            @Override
            public void run() {
                mHomeAdapter.notifyDataSetChanged();
            }
        });

        return mShowItems;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);




    }
}
